/**
 * @author Daniel Monteiro
 * */

package br.odb.bzk3.android;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

//import android.opengl.GLES20;

public class Square {

	public FloatBuffer vertexBuffer;
	FloatBuffer colorBuffer;
	public float vertices[];

	float[] color;

	private float offset;

	public float getColor(int i) {
		return color[i] + offset;
	}

	public void setColor(int i, float Color) {
		this.color[i] = Color;
	}

	// ------------------------------------------------------------------------------------------------------------
	public String toString() {
		String toReturn = new String("");
		toReturn += String.valueOf(this.vertices[0]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[1]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[2]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[3]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[4]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[5]);

		toReturn += "-";
		toReturn += String.valueOf(this.color[0]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[1]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[2]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[3]);

		return toReturn;
	}

	// ------------------------------------------------------------------------------------------------------------
	public Square() {
		vertices = new float[12];
		color = new float[16];
		color[0] = (float) Math.random();
		color[1] = (float) Math.random();
		color[2] = (float) Math.random();
		color[3] = 1.0f;
		
		color[4] = (float) Math.random();
		color[5] = (float) Math.random();
		color[6] = (float) Math.random();
		color[7] = 1.0f;
		
		color[8] = (float) Math.random();
		color[9] = (float) Math.random();
		color[10] = (float) Math.random();
		color[11] = 1.0f;
		
		color[12] = (float) Math.random();
		color[13] = (float) Math.random();
		color[14] = (float) Math.random();
		color[15] = 1.0f;
		
	}

	// ------------------------------------------------------------------------------------------------------------
	public void setColor(float R, float G, float B, float A) {
		color[0] = R;
		color[1] = G;
		color[2] = B;
		color[3] = A;
		
		color[4] = R;
		color[5] = G;
		color[6] = B;
		color[7] = A;

		color[8] = R;
		color[9] = G;
		color[10] = B;
		color[11] = A;

		color[12] = R;
		color[13] = G;
		color[14] = B;
		color[15] = A;	
		
		ByteBuffer byteBuf = ByteBuffer.allocateDirect( color.length * 4 );
		byteBuf.order(ByteOrder.nativeOrder());		
		colorBuffer = byteBuf.asFloatBuffer();
		colorBuffer.put( color );
		colorBuffer.position(0);
			
	}

	// ------------------------------------------------------------------------------------------------------------
	public void draw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}

	

	// ------------------------------------------------------------------------------------------------------------

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(float offset) {
		this.offset = offset;
	}

	/**
	 * @return the offset
	 */
	public float getOffset() {
		return offset;
	}
//
//	public void drawGLES2( int vertexHandle, int colorHandle ) {
//		GLES20.glVertexAttribPointer( vertexHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer );
//		GLES20.glEnableVertexAttribArray( vertexHandle );
//		GLES20.glVertexAttribPointer( colorHandle, 4, GLES20.GL_FLOAT, false, 0, colorBuffer );
//		GLES20.glEnableVertexAttribArray( colorHandle );
//		
//		GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4 );
//	}
}
